import java.io.*;
import java.util.*;

/** @author Chris Howson & Steve Horsman */
class MatrixList extends Matrix
{
    /**The adjacency list provided to store the sparse matrix in*/
	private List[] adjlist;
    
    /**Contructors*/
    public MatrixList() { }
    public MatrixList(int m, int n)
    {
        //initialises adjlist to be an size m array of lists
        adjlist = new ArrayList[m];
        
        for(int i = 0; i < m; i++)
        {
            adjlist[i] = new ArrayList<Element>();
        }
        setRows(m);
        setCols(n);
        setType("list");
    }

    /**Insert a value into the matrix*/
    private void insert(List<Element> row, int col, double data)
    {
        boolean found = false;
        for(int i = 0; i < row.size(); i++) //while
        {
            if(row.get(i).getCol() == col)
            {
                row.set(i, new Element(col,data));
                found = true;
                break;
            }
        }
        if(!found)
        {
            row.add(new Element(col,data));
        }
    }
    
    /**Retrieve a value from a cell*/
    @SuppressWarnings("unchecked")
    double getCell(int row, int col)
    {
        return searchrow(adjlist[row], col);
    }
    
    /**Search a row for an entry*/
    double searchrow(List<Element> l, int col)
    {
        for(int i = 0; i < l.size(); i++)
        {
            if(l.get(i).getCol() == col)
            {
                return l.get(i).getData();
            }
        }
        return 0;
    }
    
    /**Set a value in a cell*/
    @SuppressWarnings("unchecked")
    void setCell(int row, int col, double data)
    {
    	if (data != 0)
    	{
    		insert(adjlist[row], col, data);
    	}
    }
    
    /**Remove a value from a cell*/
    @SuppressWarnings("unchecked")
    void deleteCell(int row, int col)
    {
    	insert(adjlist[row], col, 0);
    }
    
    /**Load a text file into an array*/
    public MatrixList load_array(String filename)
    {
        BufferedReader in = null;
        String templine;
        ArrayList<String> lines = new ArrayList<String>();
        String[] array;
        String individualline;
        try
        {
            //Create file readers
            FileReader f_reader= new FileReader (filename);
            in = new BufferedReader(f_reader);
            
            //While no more lines, read in new line and append to one string
            while((templine = in.readLine()) != null)
            {
                lines.add(templine);
            }
            //Set size of new matrix
            individualline = lines.get(0);
            array = individualline.split(" ");
            MatrixList newmatrix = new MatrixList(Integer.parseInt(""+ array[0]), Integer.parseInt(""+ array[1]));
            
            //Set rest of matrix up
            for(int i = 1; i < lines.size(); i++)
            {
                individualline = lines.get(i);
                array = individualline.split(" ");
                newmatrix.setCell((Integer.parseInt(""+ array[0])-1), (Integer.parseInt(""+ array[1])-1), Integer.parseInt(""+ array[2]));
            }
            return newmatrix;
        }
        catch (Exception e)
        {
            System.err.println("Error " + e);
        }
        return null;
    }
    
    /**Returns the data type of this matrix*/
    String getType()
    {
        return "MatrixList";
    }
}

